package com.example.dynamicdatasourcetest.controller;

import com.example.dynamicdatasourcetest.bean.User;
import com.example.dynamicdatasourcetest.datasource.DynamicDataSourceContextHolder;
import com.example.dynamicdatasourcetest.enumeration.DataSourceType;
import com.example.dynamicdatasourcetest.pojo.HttpResonse;
import com.example.dynamicdatasourcetest.service.UserService;
import com.example.dynamicdatasourcetest.util.DynamicDataSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/testDatasource")
public class TestController {

    @Autowired
    private UserService userService;
    @Autowired
    private DynamicDataSourceUtil dataSourceUtil;

    @GetMapping(value = "/getData/{dsType}")
    public HttpResonse getData(@PathVariable("dsType") String dsType) {
        //http://localhost:8888/testDatasource/getData/master

        if (dataSourceUtil.checkDataSourceType(dsType)){
            DynamicDataSourceContextHolder.setDataSourceType(dsType);
        }
        List<User> list = userService.list();
        return new HttpResonse().successful(list);
    }
}

